package server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import classes.User;
import db.DB;
import db.DBmanager;

/**
 * Servlet implementation class BuyServlet
 */
public class BuyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public BuyServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			System.out.println("BuyServlet");
			String lat=request.getParameterValues("lat")[0];
			String lng=request.getParameterValues("lng")[0];
			String userName=request.getParameterValues("userName")[0];
			String url = "http://ws.geonames.org/findNearbyStreetsOSM?lat="+lat+"&lng="+lng;

			//HttpSession sess= request.getSession();
			//String userName=(String)sess.getAttribute("userName");



			User user=DBmanager.getDB().getUserByUserName(userName);
			//**** getting user curr location+checking if the new location resulted in a new action(street open for sale, street have owner...)
			String userArea=StreetsNameHelper.getTextDateByUrl(url);
			System.out.println(userArea);
			String startTag = "<name>";
			String endTag = "</name>";
			int start = userArea.indexOf(startTag)+startTag.length();
			int end = userArea.indexOf(endTag);
			
			String startTagLine = "<line>";
			String endTagLine = "</line>";
			int startLine=userArea.indexOf(startTagLine)+startTagLine.length();
			int endLine = userArea.indexOf(endTagLine);
			
			String startTagDis = "<distance>";
			String endTagDis = "</distance>";
			int startDis = userArea.indexOf(startTagDis)+startTagDis.length();
			int endDis = userArea.indexOf(endTagDis);
			
			String buyResult="";
			String userLocation="";
			PrintWriter out=response.getWriter();
			if(start>0 && end>0 && startLine>0 && endLine>0 && startDis>0 && endDis>0 ){
				Double dis=Double.parseDouble(userArea.substring(startDis,endDis));
				System.out.println("******distance from knowen street: "+dis);
				if (dis>0.01){
					out.write("You are not placed on a street currently");
					response.setStatus(400);
					return;
				}
				userLocation = userArea.substring(start, end);
				String coordinates= userArea.substring(startLine,endLine);
				buyResult=DBmanager.getDB().buyStreet(userLocation,userName,user.getUniqueKey());
				DBmanager.getDB().insertStreet(userLocation,user.getUniqueKey(),DB.STREET_PRICE+"",DB.STREET_PANELTY+"","open",coordinates);
				out.write(buyResult);
				response.setStatus(200);
				return;
			}

		}catch(Exception e){
			e.printStackTrace();
			PrintWriter out=response.getWriter();
			out.write("we have encountered problem with the buying process, please try later");
			response.setStatus(400);
		}

	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
